//
//  AppDelegate.h
//  AccuroFit
//
//  Created by Yongfeng Li on 8/6/15.
//  Copyright (c) 2015 Nci Technology Inc. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "NcitWiFiDev.h"
#import "NcitBleDevice.h"
#import "UIBodyProfileViewCtrl.h"

#import "goalDefine.h"

#define kReg_BodySvrIdx @"Reg_BodySvrIdx"
#define kSync_LastSyncIdx @"Sync_LastSycIdx"
#define kSync_LastDldIdx @"Sync_LastDldIdx"
#define kSync_LastMemDldIdx @"Sync_LastMemDldIdx"

#define kSavedHrDevice @"StoredHrDevices"
#define _kStoredHMUuid @"StoredHMUuid"
#define _kStoredPEUuid @"StoredPEUuid"

#define _FitHR_RngMin 0.65
#define _FitHR_RngMax 0.75

#define _OneDaySeconds 24 * 60 * 60
#define _FtToMeter 0.3048
#define _KgtoLb 2.20462262
#define _LbtoKg  0.45359237
#define _IntoCm 2.54
#define _KmtoMis 0.621

#define _DaySeconds 43200.0
#define _nDurtBarNumber 5


#define _bkgrdColor [UIColor colorWithRed:0.045 green:0.045 blue:0.045 alpha:0.99]

@class N_DataGoals;

@class N_WgtData;
@class N_FatData;
@class N_BdpData;

@class H_Rate;
@class H_RateItem;
@class H_RateRng;

@class P_Steps;
@class N_GroupInfo;

@class HeaRateViewCtrl;
@class ViewController;


static char _szTagIdCheck[] = "IdCheck";


@interface AppDelegate : UIResponder <UIApplicationDelegate, NcitBleDevDelegate, NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionTaskDelegate>

@property (strong) id<GoalDataDelegate> goalDelegate;

@property(assign, nonatomic) NSInteger m_nLstHrtIdx;
@property(assign, nonatomic) NSInteger m_nLastHrtId;
@property (assign, nonatomic) short m_nLastHrate;
@property (assign, nonatomic) short m_nCurtAvgHrt;


@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *m_pNavController;
@property (strong, nonatomic) ViewController* m_pRootViewCtrl;

@property (assign, nonatomic) NSInteger m_nLoginBodyId;
@property (retain, nonatomic) NSString* m_sLoginEmail;

@property (assign, nonatomic) NSInteger m_nLastBodyId;
@property (retain, nonatomic) NSString* m_sLastBodyName;

@property (retain, nonatomic, strong) LoginBodyInfo* m_bodyInfo;
@property (retain, nonatomic) NSCalendar* m_calendar;

@property (assign, nonatomic) BOOL m_bHostConn;
@property (retain, nonatomic) NcitWiFiDev* m_ncitWiFiDev;

@property (assign, nonatomic) BOOL m_bSupportBle;
@property (retain, nonatomic) NcitBleDevice* m_ncitBleDev;

@property (assign, nonatomic) float m_fStepLength;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (retain, nonatomic) NSDateFormatter* dtFmt;
@property (assign, nonatomic) BOOL m_bStoredBleUuid;

@property (retain, nonatomic) H_Rate* m_pHrate;
@property (assign, nonatomic) NSInteger  m_nHrateId;

@property (assign, nonatomic) BOOL m_bClearCoreData;
@property (assign, nonatomic) BOOL m_bDownloadHrmMemory;


+ (BOOL) isLanguageChines;
- (BOOL) isNetWorkConnAvailable;


//call webapi
- (NSMutableURLRequest*) requestWebApiRouter:(NSString*)sHttpMethod url:(NSString*)sUrl data:(NSDictionary*)dicData;

- (void) setUserLoginInfo:(NSArray*)jsonArray email:(NSString*)sEmail;
- (void) saveRegBodyInfo:(NSInteger)nBodyId email:(NSString*)sEmail first:(NSString*)sFirst last:(NSString*)sLast tagId:(NSString*)sTagId;
- (void) storeRegBodyInfo:(NSInteger)nBodyId tag:(NSString*)sTagId gerder:(Byte)nGender born:(NSDate*)dtBorn wgt:(float)fWgt wun:(Byte)nWu
                      hgt:(float)fHgt hun:(Byte)nHu lvl:(Byte)nActLvl email:(NSString*)sEmail first:(NSString*)sFirst last:(NSString*)sLast
                    store:(NSString*)sStore;

- (void) storeRegInfoInUserDefaults;
- (void) getRegInfoInUserDefaults;

- (BOOL) isBodyLogged;
- (void) clearRegData;
- (void) userLogout;
- (void) clearStoreData;

//interface func
- (void) enterMainPage;
- (UIViewController*) enterPageWithRes:(NSString*) sResId;
- (UIImage*) getImageScale:(NSString*) sImgName scale:(float)fRate;
- (UIImage*) imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize;

//remote server ip addr
- (char*) getRemoteSvrIp;
- (void) resetBodyId:(NSInteger)nBodyId;
- (void) resetBodyInfo;

//socket
- (NSString *) connedWifiSSID;
- (BOOL) isNcitDeviceConned;
- (BOOL) isNcitDeviceWithSsid:(NSString*) sConnSsid;

- (Boolean) ConnScaleHost:(BOOL)bConn IpAddr:(char*) szIpAddr;
- (BOOL) ConnWiFiScaleHost:(BOOL)bConn IpAddr: (char*) szIpAddr Port:(uint16_t)nPort;
- (void) resetHostConn;
- (void) CloseScaleHost;

//core data
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

//db object
- (NSUInteger) getHrateRcdCount;

- (N_DataGoals*) saveGoalData:(NSInteger)nBoydId data:(float)fValue dvUn:(short)nUn unitType:(short)nType toSvr:(BOOL)bToSvr;
- (void) storeRegGoalData:(float)fGoalWgt wun:(int) nWu dist:(float)fDist dun:(int)nDu step:(NSInteger)nSteps
                   maxBpm:(int)nMaxBpm minBpm:(int)nMinBpm;

- (void) setUserMgr:(int) nMgr admin:(NSInteger)nClubAdmin;
- (BOOL) isUserMgr;

- (N_WgtData *) insertWgtData:(NSString*)sn card:(NSString*)cn data:(float)fData unit:(short)iUnit
                       bodyId:(NSInteger)nBodyId dtScale:(NSString*)sTime iOStime: (Boolean) bOsTime toSvr:(BOOL)bToSvr;

- (NSInteger) insertWiFiWgtData: (NSInteger)nMeasId DevId:(NSString*)sn card:(NSString*)cn data:(float)fData unit:(short)iUnit
                         bodyId:(NSInteger)nBodyId dtScale:(NSString*)sTime;

- (BOOL) insertSyncWgtData:(NSNumber*)nMeasId dtScale:(NSString*)sTime sn:(NSString*)sSn data:(NSNumber*)nWgtVal unit:(NSNumber*)nUnit;

- (N_FatData *) insertFatData:(NSInteger)nBodyId fat:(float)fatVal wat:(float)watVal
                          hgt:(float)bodyHgt unit:(short)hUn oth:(long long)nOth date:(NSDate*)dtMeas toSvr:(BOOL)bToSvr;
- (BOOL) insertSyncFatData:(NSNumber*)nMeasId date:(NSString*)sTime fat:(NSNumber*)nFat wat:(NSNumber*)nWat;
                      //oth1:(NSNumber*)nOth1 oth2:(NSNumber*)nOth2;

- (H_Rate*) insertHrateData:(BOOL)bSureNew bodyId:(NSInteger)nBodyId hrateId:(NSInteger*) pHrateId time:(NSDate*)dtStart toSvr:(BOOL)bToSvr;
- (BOOL) insertSyncHrateData:(NSInteger)nBodyId hrateId:(NSInteger)nHrateId start:(NSDate*)dtStart end:(NSDate*)dtEnd mem:(BOOL)bMem act:(short)nAct;

- (H_RateRng *) insertHrateRngData:(NSInteger)nHrateId bodyId:(NSInteger)nBodyId idx:(short)nIdx
                              rng:(NSString*)sRng dur:(float)fMinute cal:(float)fCal dtVal:dtMeas toSvr:(BOOL)bToSv;
- (H_RateItem *) insertHrateItemData:(NSInteger)nHrateId bodyId:(NSInteger)nBodyId hRate:(short)nHrate dtime:(NSDate*)dtMeas toSvr:(BOOL)bToSvr;

- (H_Rate*) isDurHrateItemDateRow:(NSInteger)nBodyId measTime:(NSDate*)dtMeas;
- (H_Rate*) loadHrateStartRcd:(NSInteger)nBodyId  start: (NSDate*)dtStart;


- (H_RateItem *) insertSyncHrateItemData:(NSInteger)nBodyId hrateId:(NSInteger)nHrateId itemId:(NSInteger)nItemId hRate:(short)nHrate dtime:(NSDate*)dtMeas;
//user mgr
- (N_GroupInfo *) insertMembGroupInfo:(NSInteger)nGrpId bodyId:(NSInteger)nBodyId sn:(NSString*)sSn card:(NSString*)sCard email:(NSString*)sEmail
                                chest:(NSString*)sChest waist:(NSString*)sWaist sex:(NSString*)sSex age:(NSString*)sAge wgt:(NSString*)sWgt wun:(NSString*)sWun
                                  hgt:(NSString*)sHgt hun:(NSString*)sHun isAhtlet:(NSString*) sAthlet;
- (BOOL) updateMembGroupInfo:(NSInteger)nBodyId sn:(NSString*)sSn card:(NSString*)sCardNo email:(NSString*)sName chest:(NSString*)sChest waist:(NSString*)sWaist
                         sex:(NSString*)sSex age:(NSString*)sBorn wgt:(NSString*)sWgt wun:(NSString*)sWun hgt:(NSString*)sHgt hun:(NSString*)sHun fitLvl:(NSString*)sAth;
- (BOOL) updateMembBodyInfo:(NSInteger)nBodyId tagId:(NSString*)sTagId email:(NSString*)sEmail rmv:(BOOL)bRmv;

- (BOOL) updHrateStartEndTime:(H_Rate*)hRate startTime:(NSDate*)dtStart endTime:(NSDate*)dtEnd toSvr:(BOOL)bToSvr;
- (BOOL) fixHrateStartEndTime:(NSString*)sRateId startTime:(NSDate*)dtStart endTime:(NSDate*)dtEnd;

- (BOOL) updHrateSyncLastIdx:(NSString*)sHrateId lastIdx:(NSInteger)nIdx;
- (BOOL) updHrateSyncState:(NSInteger)nIniId toId:(NSInteger)nNewId lastIdx:(NSInteger)nIdx;
- (BOOL) updateDldsHreteRecved:(NSInteger)nBodyId;
- (BOOL) updateHrateToSvrStateforLastIdx:(NSInteger)nBodyId hRateId:(NSInteger) hRateId lastIdx:(NSInteger)nIdx;

- (BOOL) checkExistsHrateTimeRcd:(NSInteger)nBodyId hrateId:(NSInteger)nHrateId time:(NSDate*) dtStart;
- (void) updateSvrHrateId:(NSInteger)nBodyId oldId:(NSInteger)nOldId newId:(NSInteger)nNewId;

- (BOOL) updateHrateDataHrateId:(NSInteger)nIniId toId:(NSInteger)nNewId;
- (BOOL) updateHrateItemHrateIds:(NSInteger)nIniId toId:(NSInteger)nNewId;
- (BOOL) updateHrateDataActType:(NSString*)sHrateId actType:(short)nActType;
- (void) refreshCoreDataItems:(NSManagedObject *)object;

- (N_BdpData *) insertBdpData:(NSInteger)nBodyId bph:(short)nBph bpl:(short)nBpl hrt:(short)nPul date:(NSDate*)dtMeas toSvr:(BOOL)bToSvr;
- (BOOL) insertSyncBdpData:(NSNumber*)nMeasId date:(NSString*)sTime  hVal:(NSNumber*)nBlpHval lVal:(NSNumber*)nBlpLval bpm:(NSNumber*)nBlpBpm;


- (P_Steps *) insertStepsData:(NSInteger)nBodyId step:(int)nSteps dist:(float)fDist calory:(float)fCalories date:(NSDate*) dtMeas 
                          add:(BOOL)bAdd toSvr:(BOOL)bToSvr;
- (BOOL) insertSyncStepsData:(NSNumber*)nMeasId date:(NSString*)sTime  step:(NSNumber*)nSteps dist:(NSNumber*)nDist calory:(NSNumber*)nCalory;

//load
- (N_DataGoals*) loadGoalData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo unitType:(short)nType last:(BOOL)bLast;

- (NSMutableArray*) loadWgtData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc last:(BOOL)bLast;
- (NSMutableArray*) loadFatData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc last:(BOOL)bLast;

- (NSMutableArray*) loadHrateInfo:(NSInteger)nHrateId;
- (NSMutableArray*) loadHrateData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc last:(BOOL)bLast syncDld:(BOOL)bSync;
- (NSMutableArray*) loadHrateRngData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc last:(BOOL)bLast syncDld:(BOOL)bSync;
- (NSMutableArray*) loadHrateItem:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc avg:(BOOL) bAvg last:(BOOL)bLast sync:(BOOL)bSync;

- (NSInteger) loadLastHrateId:(NSInteger)nBodyId sync:(BOOL)bSync;
- (short) loadHrateItemAvg:(NSInteger)nBodyId hRateId:(NSInteger)nHrateId;

- (NSMutableArray*) loadHrateRngInfo:(NSInteger)nHrateId;
- (NSMutableArray*) loadHrateItemData:(NSInteger)nHrateId last:(BOOL)bLast;
- (NSMutableArray*) loadHrateItemLastData:(NSInteger)nHrateId dtLast:(NSDate*)dtLastDate;
- (NSMutableArray*) loadRateRngIdxData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc mon:(BOOL)bMon last:(BOOL)bLast;

- (NSMutableArray*) loadBdpData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc last:(BOOL)bLast;
- (NSMutableArray*) loadBdpAvgData:(NSInteger)nBodyId last:(NSUInteger)nCount;

- (NSMutableArray*) loadStepsData:(NSInteger)nBodyId from:(NSDate*)sFr to:(NSDate*)sTo sort:(BOOL)bAsc last:(BOOL)bLast;

//load user core data
- (NSMutableArray*) loadMemberUserInfo:(NSInteger)nBodyId grpId:(NSInteger)nGroupId;
- (N_GroupInfo*) loadMemberUserData:(NSInteger)nBodyId;

//delete operate
- (void) deleteRow:(NSString*)sTable key:(NSInteger)nKey;

- (void) sendWfbsDataToSvr:(NSInteger)nBodyId  measId:(NSInteger)nMeasId sn:(NSString*)sSn wgt:(float)fWgt wu:(short)nWu measTime:(NSDate*) dtMeas
                       fat:(float)fFat wat:(float)fWat hgt:(float)fHgt hu:(short)hUn oth:(long long)nOth
                       bph:(short)nBph bpl:(short)nBpl hrt:(short)nPul
                       stp:(int)nSteps dist:(float)fDist calory:(float)fCalorie type:(short) nType;

- (NSString*) getHourMinSec:(double)dSecds;
//- (NSMutableArray*) loadLastMeasData:(NSInteger)nBodyId;

- (void) removeCoreData:(NSString*)sTable;

- (BOOL) dateIsToday:(NSDate*) dtMeas;

//temp
//- (NSInteger) getNextHrateId;

//upd reg info
- (BOOL) updBodyRegHgt:(CGFloat)fHgtVal bFtUnit:(BOOL)bHgtFt;
- (BOOL) updTagIdDevTimeZone:(NSString*)sTagId devId:(NSString*)sDevId;
- (BOOL) updBodyRegGender:(short)nSex;
- (BOOL) updBodyRegActLvl:(short)nActLvl;
- (BOOL) updBodyRegInfo:(short)nType info:(NSString*)sInfo unit:(short)nUnit;
- (BOOL) updMaxBpmVal:(NSString*)sInfo unit:(short)nUnit view:(UIViewController*)pView;
- (BOOL) updSvrIpAddress:(short)nSvrIdx;

//other assist func
- (short) calHrateFitnessRng:(BOOL)bSetting;
- (int) getHrateRngIdx:(short)nHrate maxLine:(float)yMaxLine count:(short)nBarCount;

- (void) getUserGoalData:(BOOL)bStore;

- (void) DeleteEnableDownload:(BOOL)bEnable;
- (void) showLogingInToolBar;
- (void) showAlertBox:(NSString*) sTitle msg:(NSString*) sMsg;

//debug
- (BOOL) checkExistsHrateStart:(NSInteger)nBodyId start:(NSDate*)dtStart;
//- (int) getBodyGoalStep;
@end

